﻿//
// ToDoList v1.1
// Coder : Onat Yigit Mercan
// http://onatm.wordpress.com
//
using System;
using System.Windows;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ToDoList
{
    /// <summary>
    /// Interaction logic for Main.xaml
    /// </summary>
    public partial class ToDoWin : Window
    {
        //
        // Fields
        //
        #region fields

        // Notify icon
        private NotifyIcon notify = new NotifyIcon
        {
            Text = "ToDo List",
            // Should get application folder to avoid icon error
            Icon = new Icon(
                System.Windows.Forms.Application.StartupPath.ToString() + "/icon.ico"),
            Visible = true
        };

        // Context menu for notify icon
        private ContextMenuStrip notifyContextMenu =
            new ContextMenuStrip { Name = "notifyContextMenu" };

        private ToolStripMenuItem configurationToolStripMenuItem =
            new ToolStripMenuItem
            {
                Name = "configurationToolStripMenuItem",
                Text = "Configuration..."
            };

        private ToolStripMenuItem lockToolStripMenuItem =
            new ToolStripMenuItem { Name = "lockToolStripMenuItem", Text = "Lock" };

        private ToolStripMenuItem unlockToolStripMenuItem =
            new ToolStripMenuItem
            {
                Name = "unlockToolStripMenuItem",
                Text = "Unlock",
                Checked = true
            };

        private ToolStripMenuItem exitToolStripMenuItem =
            new ToolStripMenuItem { Name = "exitToolStripMenuItem", Text = "Exit" };

        private ToolStripSeparator separator0ToolStrip = new ToolStripSeparator();
        private ToolStripSeparator separator1ToolStrip = new ToolStripSeparator();

        private string fullFilePath { get; set; }

        #endregion

        public ToDoWin()
        {
            InitializeComponent();
            SetLocation();
            NotifyIcon();
            CheckFile();
            LoadFile();
        }

        // Need to set window location on different resolutions
        // It should be on bottom left corner
        private void SetLocation()
        {
            var height = System.Windows.SystemParameters.PrimaryScreenHeight;
            var width = System.Windows.SystemParameters.PrimaryScreenWidth;
            this.Top = (height / 768) * 500;
            this.Left = (width / 1024) * 20;
        }

        // Notify icon initialization
        private void NotifyIcon()
        {
            // Bind tool strip items to context menu
            notifyContextMenu.Items.AddRange(
                new ToolStripItem[] { configurationToolStripMenuItem, 
                    separator0ToolStrip, lockToolStripMenuItem,
                    unlockToolStripMenuItem, separator1ToolStrip,
                    exitToolStripMenuItem });

            // Create tool strip events

            configurationToolStripMenuItem.Click += configurationToolStripMenuItem_Click;
            lockToolStripMenuItem.Click += lockToolStripMenuItem_Click;
            unlockToolStripMenuItem.Click += unlockToolStripMenuItem_Click;
            exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;

            // Bind context menu to notify icon
            notify.ContextMenuStrip = notifyContextMenu;

            // Crete notify icon event
            notify.Click += notify_Click;
        }

        //
        // File methods
        //
        #region file methods

        private void CheckFile()
        {
            // Get mydocuments path
            string FilePath = Environment.GetFolderPath(
                Environment.SpecialFolder.MyDocuments);
            string FileName = "todo.txt";

            // Combine file name and mydocuments path
            fullFilePath = Path.Combine(FilePath, FileName);

            // Create file if it doesn't exist
            if (!File.Exists(fullFilePath))
            {
                File.Create(fullFilePath);
            }
        }

        // Load and check if there is a problem
        private void LoadFile()
        {
            try
            {
                txtToDo.Text = File.ReadAllText(fullFilePath);
            }
            catch (System.IO.IOException)
            {
                System.Windows.Forms.MessageBox.Show("ToDo file is corrupted",
                    "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Write todos to file
        private void WriteToFile()
        {
            File.WriteAllText(fullFilePath, txtToDo.Text);
        }

        #endregion

        //
        // Event handlers
        //
        #region event handlers

        private void notify_Click(object sender, EventArgs e)
        {
            ToDoWindow.Focus();
        }

        private void configurationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Configuration configurationWindow = new Configuration(this);
            configurationWindow.Show();
        }

        private void lockToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtToDo.IsEnabled = false;
            lockToolStripMenuItem.Checked = true;
            unlockToolStripMenuItem.Checked = false;
            WriteToFile();
        }

        private void unlockToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtToDo.IsEnabled = true;
            unlockToolStripMenuItem.Checked = true;
            lockToolStripMenuItem.Checked = false;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WriteToFile();
            notify.Dispose();
            this.Close();
        }

        #endregion
    }
}